package com.airbnb.mvrx.sample.features.clickread.views

import android.content.Context
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.airbnb.epoxy.CallbackProp
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.airbnb.mvrx.sample.Utils
import com.airbnb.mvrx.sample.core.UpApplication
import com.example.mvrxstudy.R
import com.yt.ytdeep.client.dto.ClickReadPage

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class PageContentViewPager @JvmOverloads constructor(context: Context) : FrameLayout(context) {

    private val mPager: ViewPager
    private val mAdapter: ContentAdapter
    private var scrollListener: SnappingRecyclerView.OnViewSelectedListener? = null


    init {
        inflate(context, R.layout.view_pager_content, this)
        mPager = findViewById(R.id.vp_pager)
        mAdapter = ContentAdapter()
        mPager.adapter = mAdapter
        mPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                if (scrollListener != null) {
                    scrollListener!!.onSelected(mPager, p0)
                }
            }

        })

    }

    override fun setLayoutParams(params: ViewGroup.LayoutParams?) {
        params!!.height = Utils.getContentHeight(context) - Utils.dp2px(context, 62) - Utils.dp2px(context, 56)
        super.setLayoutParams(params)
    }


    @ModelProp
    fun setContent(pages: List<ClickReadPage>) {
        mAdapter.mPages = pages
        mAdapter.notifyDataSetChanged()
    }

    @ModelProp
    fun setPosition(position: Int) {
        mPager.setCurrentItem(position, false)
    }

    @CallbackProp
    fun setOnScrollListener(listener: SnappingRecyclerView.OnViewSelectedListener?) {
        Utils.log("CRContentView %s", listener)
        scrollListener = listener
    }

    class ContentAdapter : PagerAdapter() {

        var mPages: List<ClickReadPage>? = null

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val item = CRContentItem(container.context)
            item.setImage(Utils.imageUrlFromResId(UpApplication.getApp().BASE_URL,
                    mPages!![position].imgResId, mPages!![position].imgResSign))
            container.addView(item)
            return item
        }

        override fun isViewFromObject(v: View, `object`: Any): Boolean {
            return v === `object`
        }

        override fun getItemPosition(`object`: Any): Int {
            return PagerAdapter.POSITION_NONE
        }


        override fun getCount(): Int {
            return if (mPages != null) mPages!!.size else 0
        }

        override fun destroyItem(parent: ViewGroup, position: Int, `object`: Any) {
            parent.removeView(`object` as View)
        }

    }


}